package com.weayer.constraints;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 自定义类型校验实现类
 * @author: Weayer
 **/
public class DicCheckValidator implements ConstraintValidator<DicCheck, Object> {
    private List<String> dicValue = null;

    @Override
    public void initialize(DicCheck constraintAnnotation) {
        dicValue = Arrays.asList(constraintAnnotation.dicValue());
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Object s, ConstraintValidatorContext constraintValidatorContext) {
        if((s instanceof String) && dicValue.contains(s)){
            return true;
        }
        if(s instanceof Integer){
            return dicValue.contains(String.valueOf(s));
        }
        return false;
    }
}
